package com.sand.service.Imp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sand.dao.IBaseDao;
import com.sand.entity.TRoleEntity;
import com.sand.entity.TRoleauthEntity;
import com.sand.service.IRoleService;
import com.sand.util.PageModel;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service(value = "roleService")
public class RoleServiceImp implements IRoleService {
/**
 *
 *
 * @author <achao1441470436@gmail.com>
 * @since 2021/11/28/028 21:04
 */
    //Dao层接口
    @Resource(name = "baseDao")
    private IBaseDao baseDao;

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<TRoleEntity> queryRoleAll() {
        return baseDao.queryObject("from TRoleEntity");
    }
    //根据id查询角色
    @Override
    public TRoleEntity queryRoleById(int id) {
        return (TRoleEntity) baseDao.queryObjectById(TRoleEntity.class, id);
    }
    //分页查询角色
    @Override
    public PageModel<TRoleEntity> queryRoleLimit(int limit, int page, String condition) {
        PageModel<TRoleEntity> pm = new PageModel<TRoleEntity>();
        //计算开始位置
        int begin=(page-1)*limit;
        //创建查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(TRoleEntity.class);
        //添加模糊查询条件
        if(condition!=null){
            dc.add(Restrictions.like("name","%"+condition+"%"));
        }

        List<Long> list1 = baseDao.queryObjectCommon(dc.setProjection(Projections.rowCount()));
        dc.setProjection(null);
        dc.addOrder(Order.desc("rid"));
        List<TRoleEntity> list = baseDao.queryObjectLimit(dc, begin, limit);
        long i=list1.get(0);
        int count= (int) i;

        pm.setList(list);
        pm.setPageNo(page);
        pm.setPageSize(limit);
        pm.setPagecount(count);
        return pm;
    }
    //添加角色
    @Override
    public boolean addRole(String data) {
        if(data!=null){
            TRoleEntity role = new TRoleEntity();
            role.setName(data);
            boolean bln = baseDao.addObject(role);
            if(bln)return true;
        }
        return false;
    }
    //删除角色
    @Override
    public boolean delRole(String data) {
        if(data!=null){
            //解析json数据
            List<TRoleEntity> list = JSON.parseArray(data, TRoleEntity.class);
            boolean bln = baseDao.delAllObject(list);
            if(bln) return true;
        }
        return false;
    }
    //修改角色
    @Override
    public boolean updateRole(String data) {
        if(data!=null){
            TRoleEntity role = JSON.parseObject(data, TRoleEntity.class);
            boolean bln = baseDao.updateObject(role);
            if(bln)return true;
        }
        return false;
    }
    //修改角色拥有的权限
    @Override
    public boolean updateRoleAndAuthority(int id, String data) {
        //删除角色已拥有的权限
        List<TRoleauthEntity> list1 = baseDao.queryObject("from TRoleauthEntity tra where tra.roleId=" + id);
        if(list1!=null) {
            baseDao.delAllObject(list1);
        }

        //获得选择的权限
        ArrayList<Integer> list = new ArrayList<>();
        if(data!=null){
            JSONArray array = JSONArray.parseArray(data);
            for (int i = 0; i < array.size(); i++) {
                JSONObject obj = JSONObject.parseObject(array.get(i).toString());
                list.add(Integer.parseInt(obj.get("id").toString()));
                JSONArray array1 = JSONArray.parseArray(obj.get("children").toString());
                for (int j = 0; j < array1.size(); j++) {
                    JSONObject obj2 = JSONObject.parseObject(array1.get(j).toString());
                    list.add(Integer.parseInt(obj2.get("id").toString()));
                    JSONArray array2 = JSONArray.parseArray(obj2.get("children").toString());
                    for (int k = 0; k < array2.size(); k++) {
                        JSONObject obj3 = JSONObject.parseObject(array2.get(k).toString());
                        list.add(Integer.parseInt(obj3.get("id").toString()));
                        JSONArray array3 = JSONArray.parseArray(obj3.get("children").toString());
                        for (int m = 0; m < array3.size(); m++) {
                            JSONObject obj4 = JSONObject.parseObject(array3.get(m).toString());
                            list.add(Integer.parseInt(obj4.get("id").toString()));
                        }
                    }
                }
            }
        }
        //将选中的权限加入角色
        if(list!=null) {
            for (Integer i : list) {
                TRoleauthEntity rAuth = new TRoleauthEntity();

                rAuth.setRoleId(id);
                rAuth.setAuthId(i);
                boolean bln2 = baseDao.addObject(rAuth);
                if(!bln2) {
                    return false;
                }
            }
        }
        return true;
    }
}
